package com.example.foobar.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.foobar.business.service.DaOfficialAccountService;
import com.example.foobar.business.service.ImsAccountWechatsService;
import com.example.foobar.entity.DaOfficialAccount;
import com.example.foobar.entity.ImsAccountWechats;
import com.example.foobar.dao.service.DaOfficialAccountDaoService;
import com.example.foobar.dao.service.ImsAccountWechatsDaoService;
import io.geekidea.springbootplus.framework.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import com.example.foobar.param.DaOfficialAccountPageParam;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.framework.core.validator.groups.Add;
import io.geekidea.springbootplus.framework.core.validator.groups.Update;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 本地公众号信息 控制器
 *
 * @author 郑英奔
 * @since 2021-05-31
 */
@Slf4j
@RestController
@RequestMapping("/daOfficialAccount")
@Module("daOfficialAccountController")
@Api(value = "本地公众号信息API", tags = {"本地公众号信息"})
public class DaOfficialAccountController extends BaseController {

    @Autowired
    private DaOfficialAccountService daOfficialAccountService;

    /**
     * 添加本地公众号信息
     */
    @PostMapping("/add")
    @OperationLog(name = "添加本地公众号信息", type = OperationLogType.ADD)
    @ApiOperation(value = "添加本地公众号信息", response = ApiResult.class)
    public ApiResult<Boolean> addDaOfficialAccount(@Validated(Add.class) @RequestBody DaOfficialAccount daOfficialAccount) throws Exception {
        boolean flag = daOfficialAccountService.saveDaOfficialAccount(daOfficialAccount);
        return ApiResult.result(flag);
    }

    /**
     * 修改本地公众号信息
     */
    @PostMapping("/update")
    @OperationLog(name = "修改本地公众号信息", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改本地公众号信息", response = ApiResult.class)
    public ApiResult<Boolean> updateDaOfficialAccount(@Validated(Update.class) @RequestBody DaOfficialAccount daOfficialAccount) throws Exception {
        boolean flag = daOfficialAccountService.updateDaOfficialAccount(daOfficialAccount);
        return ApiResult.result(flag);
    }

    /**
     * 删除本地公众号信息
     */
    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除本地公众号信息", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除本地公众号信息", response = ApiResult.class)
    public ApiResult<Boolean> deleteDaOfficialAccount(@PathVariable("id") Long id) throws Exception {
        boolean flag = daOfficialAccountService.deleteDaOfficialAccount(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取本地公众号信息详情
     */
    @GetMapping("/info/{id}")
    @OperationLog(name = "本地公众号信息详情", type = OperationLogType.INFO)
    @ApiOperation(value = "本地公众号信息详情", response = DaOfficialAccount.class)
    public ApiResult<DaOfficialAccount> getDaOfficialAccount(@PathVariable("id") Long id) throws Exception {
        DaOfficialAccount daOfficialAccount = daOfficialAccountService.getById(id);
        return ApiResult.ok(daOfficialAccount);
    }

    /**
     * 获取本地公众号信息详情
     */
    @GetMapping("/getInfoByUniacid/{uniacid}")
    @OperationLog(name = "通过uniacid获取公众号信息", type = OperationLogType.INFO)
    @ApiOperation(value = "通过uniacid获取公众号信息", response = DaOfficialAccount.class)
    public ApiResult<DaOfficialAccount> getDaOfficialAccountByUniacid(@PathVariable("uniacid") String uniacid) throws Exception {
        LambdaQueryWrapper<DaOfficialAccount> wrapper  = new LambdaQueryWrapper<>(DaOfficialAccount.class).eq(DaOfficialAccount::getUniacid, Integer.valueOf(uniacid));
        DaOfficialAccount daOfficialAccount = daOfficialAccountService.selectOne(wrapper);
        return ApiResult.ok(daOfficialAccount);
    }

    /**
     * 本地公众号信息分页列表
     */
    @PostMapping("/getPageList")
    @OperationLog(name = "本地公众号信息分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "本地公众号信息分页列表", response = DaOfficialAccount.class)
    public ApiResult<Paging<DaOfficialAccount>> getDaOfficialAccountPageList(@Validated @RequestBody DaOfficialAccountPageParam daOfficialAccountPageParam) throws Exception {
        Paging<DaOfficialAccount> paging = daOfficialAccountService.getDaOfficialAccountPageList(daOfficialAccountPageParam);
        return ApiResult.ok(paging);
    }


    @PostMapping("/getList")
    @OperationLog(name = "系统角色列表", type = OperationLogType.LIST)
    @ApiOperation(value = "系统角色列表", response = DaOfficialAccount.class)
    public ApiResult<List<DaOfficialAccount>> getRoleList() {
        if(LoginUtil.getUserId() == 1){
            return ApiResult.ok(daOfficialAccountService.list());
        }else{
            return ApiResult.ok(daOfficialAccountService.list(LoginUtil.getUserId()));
        }
    }

    /**
     * 本地公众号信息分页列表
     */
    @PostMapping("/syncOfficialAccount")
    @OperationLog(name = "同步微擎公众号", type = OperationLogType.PAGE)
    @ApiOperation(value = "同步微擎公众号", response = DaOfficialAccount.class)
    public ApiResult<Boolean> syncOfficialAccount() throws Exception {
        boolean flag = daOfficialAccountService.syncOfficialAccount();
        return ApiResult.ok(flag);
    }

}

